import { createStore } from 'redux';

// state: 仓库的全局状态
// action：触发reducer时传的参数，类型：对象，必须要有一个参数 type
// reducer: 必须要有返回值，返回值为新的state
const reducer = (state, action) => {
    const { type } = action

    if(type === 'CHANGE_USER_INFO') {
        return {
            ...state,
            userInfo: action.data
        }
    } else if(type === 'CHANGE_USER_IMG') {
        return {
            ...state,
            userInfo: {
                ...state.userInfo,
                avatar_url: action.data
            }
        }
    }

    return state
}

// 仓库的默认全局状态
const initState = {
    userInfo: {}
}


// 创建仓库
const store = createStore(reducer, initState)

// 为了测试方便 将仓库先挂载到window；
window.St = store

export default store